package com.lwlk.zdk.controller.Log;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwlk.zdk.enums.RetCode;
import com.lwlk.zdk.model.Encapsulation.EncapsulationOperationType;
import com.lwlk.zdk.model.Logger.SysLog;
import com.lwlk.zdk.service.Log.SysLogService;
import com.lwlk.zdk.util.LayuiTablePagingResult;
import com.lwlk.zdk.util.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(value = "操作日志")
@RestController
@Transactional
@RequestMapping(value = "/Log")
public class LogController {

    @Autowired
    private SysLogService sysLogService;



    @ApiOperation(value = "日志分析接口")
    @RequestMapping(value = "/show",method = RequestMethod.GET)
    public LayuiTablePagingResult show(String userName, String operation, String startTime,String endTime, Integer page, Integer size){
      LayuiTablePagingResult result=null;
      try {
          PageHelper.startPage(page,size);
              List<Map<String,Object>> selectCondition=sysLogService.selectCondition(userName,operation,startTime,endTime);
              PageInfo pageInfo=new PageInfo<>(selectCondition);
              result = new LayuiTablePagingResult();
              result.setCode(RetCode.SUCCESS.getCode());
              result.setMsg(RetCode.SUCCESS.getMsg());
              result.setCount(pageInfo.getTotal());
              result.setData(pageInfo.getList());
              Long totalPages = pageInfo.getTotal() / size;
              if (pageInfo.getTotal() % size != 0){
                  totalPages ++;
              }
              result.setTotalPages(totalPages);
              return result;
      }catch (Exception e){
          e.printStackTrace();
      }
      return result;
    }


    @ApiOperation(value = "车辆交换记录")
    @RequestMapping(value = "/vehicleSwop",method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "operation",value = "指令内容",paramType = "query"),
            @ApiImplicitParam(name = "createDate",value = "发送时间",paramType = "query"),
            @ApiImplicitParam(name = "userName",value = "操作人员",paramType = "query")})
    public LayuiTablePagingResult getVehicleSwop(Integer page,Integer size,String operation,String startTime,String endTime,String userName){
       LayuiTablePagingResult result=null;
       try {
           PageHelper.startPage(page,size);
           List<Map<String,Object>>swopList;
           if (operation!=null||startTime!=null || endTime!=null ||userName!=null){
               swopList=sysLogService.selectTerm(operation,startTime,endTime,userName);
           }else {
               swopList=sysLogService.selectSwop();
           }
           PageInfo pageInfo=new PageInfo<>(swopList);
           result = new LayuiTablePagingResult();
           result.setCode(RetCode.SUCCESS.getCode());
           result.setMsg(RetCode.SUCCESS.getMsg());
           result.setCount(pageInfo.getTotal());
           result.setData(pageInfo.getList());
           Long totalPages = pageInfo.getTotal() / size;
           if (pageInfo.getTotal() % size != 0){
               totalPages ++;
           }
           result.setTotalPages(totalPages);
           return result;
       }catch (Exception e){
           e.printStackTrace();
       }
       return result;
    }

    @ApiOperation(value = "操作类型展示")
    @RequestMapping(value = "/operationShow",method = RequestMethod.GET)
    public Response getOperationShow(){
        Response response=new Response();
        List<EncapsulationOperationType> list=sysLogService.selectOperationShow();
        response.setCode(RetCode.SUCCESS);
        response.setData(list);
        return response;
    }

}
